<template>
  <div class="register-container full-screen flex">
    <el-card class="card">
      <div class="register-head primary text-center">
        <h2>人脸识别门禁系统</h2>
      </div>
      <el-form ref="registerForm" :model="form" :rules="rules" @submit.prevent>
        <el-form-item prop="mobile">
          <el-input
              v-model="form.mobile"
              placeholder="请输入手机号"
              prefix-icon="iconfont iconemail"
          ></el-input>
        </el-form-item>


        <el-form-item prop="password">
          <el-input
              prefix-icon="iconfont iconpassword"
              v-model="form.password"
              type="password"
              placeholder="请输入密码，必须包含字母和数字，长度为6-18"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input
              prefix-icon="iconfont iconpassword"
              v-model="form.confirm"
              placeholder="请确认密码"
              type="password"
          ></el-input>
        </el-form-item>


        <el-form-item prop="name">
          <el-input
              v-model="form.name"
              placeholder="请输入姓名"
              prefix-icon="iconfont iconemail"
          ></el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-select
              v-model="form.sex"
              placeholder="请选择性别"
              style="width: 100%"
          >
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="idNumber">
          <el-input
              v-model="form.idNumber"
              placeholder="请输入身份证号码"
              prefix-icon="iconfont iconemail"
          ></el-input>
        </el-form-item>
        <el-form-item prop="college">
          <el-select v-model="form.college" placeholder="请选择学院" style="width: 100%" @change="getTeacher"
          >
            <el-option
                v-for="item in colleges"
                :key="item.title"
                :label="item.title"
                :value="item.title"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <div style="margin-top: 20px">
            <el-radio-group v-model="form.identity" size="medium">
              <el-radio-button :label=0 :key=1>学生</el-radio-button>
              <el-radio-button :label=1 :key=1>教师</el-radio-button>
<!--              <el-radio-button :label=2 :key=2>管理员</el-radio-button>-->
            </el-radio-group>
          </div>
        </el-form-item>
        <span v-show="form.identity===0&&collegeChoose!=null">
          <el-form-item prop="major">
            <el-input
                v-model="form.major"
                placeholder="请输入专业"
                prefix-icon="iconfont iconemail"
            ></el-input>
          </el-form-item>

          <el-form-item prop="uClass">
            <el-input
                v-model="form.uClass"
                placeholder="请输入班级"
                prefix-icon="iconfont iconemail"
            ></el-input>
          </el-form-item>

          <el-form-item prop="teacherId">
              <el-select v-model="form.teacherId" placeholder="请选择老师" style="width: 100%">
                <el-option
                v-for="item in teachers"
                :key="item.uid"
                :label="item.name"
                :value="item.uid"
                >
              </el-option>
            </el-select>
          </el-form-item>
        </span>

        <el-form-item>
          <el-button
              type="primary"
              native-type="submit"
              :loading="registerLoading"
              @click="onSubmit"
              style="width: 100%; margin-top: 30px"
          >注册
          </el-button
          >
        </el-form-item>
        <div class="text-center">
          <router-link to="/login" class="primary to_login"
          >已有账号，去登录<i class="el-icon-right"></i
          ></router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {register} from "/src/network/account.js";
import {teacherList, collegeList} from "/src/network/account.js";
import {Msg} from "../../utils/pubMethod";

export default {
  name: "register",
  components: {},
  data() {
    const checkConfirm = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请确认密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    const checkPassword = (rule, value, callback) => {
      const patten = /^(?=.*\d)(?=.*[a-zA-z])[a-zA-Z0-9]{6,18}$/;
      if (!patten.test(value)) {
        callback(new Error("密码不符合要求"));
      } else {
        callback();
      }
    };
    return {
      colleges: [],
      teachers: [],
      collegeChoose: null,
      form: {
        mobile: "",
        password: "",
        confirm: "",
        identity: 0,
        name: '',
        sex: "",
        idNumber: "",
        major: "",
        college: "",
        uClass: "",
        teacherId: ""
      },
      registerLoading: false,
      rules: {
        mobile: [
          {required: true, message: "请输入手机号码", trigger: "blur"},
          {min: 11, max: 11, message: "手机号码格式错误", trigger: "blur"},
        ],
        name: [
          {required: true, message: "请输入姓名", trigger: "blur"},
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {validator: checkPassword, trigger: "blur"},
        ],
        confirm: [{validator: checkConfirm, trigger: "blur"}],
        identity: [
          {required: true, message: "请选择身份", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    teacherList().then(res => {

    }, err => {
    })
    collegeList().then(res => {
      try {
        if (res.code === 200) {
          this.colleges = res.data
        }
      } catch
          (e) {
        Msg(this.$message, 'error', '网络连接出错')
      }
    }, err => {
    })
  },
  methods: {
    onSubmit() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.register();
        }
      });
    },
    register() {
      const form = this.form;
      this.registerLoading = true;
      register(form).then(
          (res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "注册成功",
              });
              this.$router.push({
                name: "Login",
              });
            } else {
              this.$message({
                type: "error",
                message: res.data,
              });
            }
            this.registerLoading = false;
          },
          (error) => {
            this.$message({
              type: "error",
              message: "注册失败",
            });
            this.registerLoading = false;
          }
      );
    },
    getTeacher(college) {
      teacherList(college).then(res => {
        try {
          if (res.code === 200) {
            this.collegeChoose = college

            this.teachers = res.data
          }
        } catch
            (e) {
          Msg(this.$message, 'error', '网络连接出错')
        }

      }, err => {
      })
    }
  },
};
</script>

<style scoped>
.register-container {
  background: url(/img/bg.jpg);
  background-size: cover;
  flex-direction: column;
  align-items: center;
  background-color: #fce0e0;

}

.register-head {
  height: 57px;
  margin: 20px auto;
  letter-spacing: 4px;
}

.card {
  opacity: 0.9;
  margin: auto;
  width: 500px;
}

.to_login {
  text-decoration: none;
}
</style>
